//
//  GFPandectViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFPandectViewController.h"

@interface GFPandectViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *flow;

@property (nonatomic, strong) NSMutableArray *urlArr;

@property (nonatomic, strong) UIView *barView;
@property (nonatomic, strong) UIView *moveView;
@end

@implementation GFPandectViewController

- (void)viewWillAppear:(BOOL)animated {
    
#pragma mark - 修改navi颜色
    /* 修改navigation title 颜色 */
    
    UIColor *color = [UIColor colorWithRed:209/255.0 green:186/255.0 blue:116/255.0 alpha:1];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];/* 在NSFontAttributeName中寻找*/
    
    self.navigationController.navigationBar.titleTextAttributes = dic;
    
}

#pragma mark _ 处理数据
- (void)handleData {
    
    NSString *frontUrlStr = @"http://food.boohee.com/fb/v1/foods?kind=";
    NSString *behindStr0 = @"&page=1&order_asc=0";
    NSString *behindStr1 = @"&page=1&order_asc=1";
    self.urlArr = [NSMutableArray array];
    
    if ([self.kindName isEqualToString:@"brand"]) {
        
        if (self.useValue.intValue == 1) {
            self.value = @"&value=20&order_by=";
        } else if (self.useValue.intValue == 12) {
            self.value = @"&value=21&order_by=";
        } else if (self.useValue.intValue == 13) {
            self.value = @"&value=23&order_by=";
        } else {
            self.value = [NSString stringWithFormat:@"&value=%d&order_by=", self.useValue.intValue - 1];
        }
        
    }
    
    for (int i = 1; i < 5; i++) {
        
        NSString *url1 = [[frontUrlStr stringByAppendingString:self.kindName] stringByAppendingString:self.value];
        NSString *iStr = [NSString stringWithFormat:@"%d", i];
        
        if (i % 2 != 0) {
            
            NSString *url = [[url1 stringByAppendingString:iStr] stringByAppendingString:behindStr0];
            
            [self.urlArr addObject:url];
            
        } else {
            
            NSString *url = [[url1 stringByAppendingString:iStr] stringByAppendingString:behindStr1];
            
            [self.urlArr addObject:url];
            
        }
    }
}


#pragma mark _ 创建头部View
- (void)creatTitleView {
    
    NSArray *array = @[@"常见", @"热量⬆︎", @"蛋白质⬇︎", @"脂肪⬆︎"];
    
    self.barView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 30)];
    //    self.barView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.barView];
    
    
    for (int i = 0; i < 4; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(ScreenWidth / 375.0 * 70 * i, 0, ScreenWidth / 375.0 * 70, ScreenWidth / 375.0 * 25);
        
        button.tag = i + 1;
        
        [button setTitle:array[i] forState:UIControlStateNormal];
        
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
        if (button.tag == 1) {
            
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        } else {
                [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        
        [button addTarget:self action:@selector(clickedButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.barView addSubview:button];
        
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenWidth / 375.0 * 29, ScreenWidth / 375.0 * 375, 1)];
    view.backgroundColor = [UIColor lightGrayColor];
    [self.barView addSubview:view];

    
    
    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenWidth / 375.0 * 25, ScreenWidth / 375.0 * 70, ScreenWidth / 375.0 * 5)];
    
    self.moveView.backgroundColor = [UIColor redColor];
    
    [self.barView addSubview:self.moveView];
    

    
    
    
}

#pragma mark _ button点击事件
- (void)clickedButton:(UIButton *)button {
    
    if ([button.titleLabel.textColor isEqual:[UIColor redColor]]) {
        
        if ([button.titleLabel.text isEqualToString:@"热量⬆︎"]) {
            
            [button setTitle:@"热量⬇︎" forState:UIControlStateNormal];
            
            NSString *str = [self.urlArr objectAtIndex:1];
            
            str = [str substringToIndex:str.length - 1];
            
            str = [str stringByAppendingString:@"0"];
            
            [self.urlArr replaceObjectAtIndex:1 withObject:str];
            
            [self.collection reloadData];
            
        } else if ([button.titleLabel.text isEqualToString:@"热量⬇︎"]) {
            [button setTitle:@"热量⬆︎" forState:UIControlStateNormal];
            
            NSString *str = [self.urlArr objectAtIndex:1];
            
            str = [str substringToIndex:str.length - 1];
            
            str = [str stringByAppendingString:@"1"];
            
            [self.urlArr replaceObjectAtIndex:1 withObject:str];
            
            [self.collection reloadData];
        } else if ([button.titleLabel.text isEqualToString:@"蛋白质⬇︎"]) {
            [button setTitle:@"蛋白质⬆︎" forState:UIControlStateNormal];
            
            NSString *str = [self.urlArr objectAtIndex:2];
            
            str = [str substringToIndex:str.length - 1];
            
            str = [str stringByAppendingString:@"1"];
            
            [self.urlArr replaceObjectAtIndex:2 withObject:str];
            
            [self.collection reloadData];
        } else if ([button.titleLabel.text isEqualToString:@"蛋白质⬆︎"]) {
            [button setTitle:@"蛋白质⬇︎" forState:UIControlStateNormal];
            
            NSString *str = [self.urlArr objectAtIndex:2];
            
            str = [str substringToIndex:str.length - 1];
            
            str = [str stringByAppendingString:@"0"];
            
            [self.urlArr replaceObjectAtIndex:2 withObject:str];
            
            [self.collection reloadData];
        } else if ([button.titleLabel.text isEqualToString:@"脂肪⬆︎"]) {
            [button setTitle:@"脂肪⬇︎" forState:UIControlStateNormal];
            
            NSString *str = [self.urlArr objectAtIndex:3];
            
            str = [str substringToIndex:str.length - 1];
            
            str = [str stringByAppendingString:@"0"];
            
            [self.urlArr replaceObjectAtIndex:3 withObject:str];
            
            [self.collection reloadData];
        } else if ([button.titleLabel.text isEqualToString:@"脂肪⬇︎"]) {
            [button setTitle:@"脂肪⬆︎" forState:UIControlStateNormal];
            
            NSString *str = [self.urlArr objectAtIndex:3];
            
            str = [str substringToIndex:str.length - 1];
            
            str = [str stringByAppendingString:@"1"];
            
            [self.urlArr replaceObjectAtIndex:3 withObject:str];
            
            [self.collection reloadData];
        }
        
    }
    
    
    CGFloat y = self.collection.contentOffset.y;
    
    
    [self.collection setContentOffset:CGPointMake((button.tag - 1) * ScreenWidth / 375.0 * 375, y) animated:YES];
    
}

#pragma mark _ 创建内容CollectionView
- (void)creatCollectionView {
    
    
    self.flow = [[UICollectionViewFlowLayout alloc] init];
    //    self.flow.itemSize = self.view.frame.size;
    self.flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.flow.minimumInteritemSpacing = 0;
    self.flow.minimumLineSpacing = 0;
    self.flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    self.flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flow];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.pagingEnabled = YES;
    
    [self.collection registerClass:[GFPandectCell class] forCellWithReuseIdentifier:@"PandectCell"];
    
    [self.view addSubview:self.collection];

}



#pragma mark _ collectionView DataSource 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.urlArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GFPandectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PandectCell" forIndexPath:indexPath];
    
    NSString *itemNumber = [NSString stringWithFormat:@"%ld", indexPath.item];
    
    cell.urlArr = self.urlArr;
    
    cell.itemNumber = itemNumber;
    
#pragma mark _ block传值
    cell.clickBlock = ^(NSString *code) {
        
        GFDetialsViewController *detailsVC = [[GFDetialsViewController alloc] init];
        
        detailsVC.food_code = code;
        
        [self.navigationController pushViewController:detailsVC animated:YES];
        
    };
    
    return cell;
}

#pragma mark _ 滑动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.collection) {
        
        self.moveView.frame = CGRectMake(scrollView.contentOffset.x / ScreenWidth * ScreenWidth / 375.0 * 70, ScreenWidth / 375.0 * 25, ScreenWidth / 375.0 * 70, ScreenWidth / 375.0 * 5);
        
        for (int i = 1; i < 5; i++) {
            UIButton *ppbutton = (UIButton *)[self.barView viewWithTag:i];
            
            if (self.moveView.frame.origin.x < ppbutton.tag * ScreenWidth / 375.0 * 70 - 0.1 && self.moveView.frame.origin.x >= (ppbutton.tag - 1) * ScreenWidth / 375.0 * 70 - 0.1) {
                
                [ppbutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                
            } else {

                    [ppbutton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
 
            }
            
            //            NSLog(@"%f", self.moveView.frame.origin.x);
            
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.titleName;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.waitView removeFromSuperview];
    [self handleData];
    [self creatTitleView];
    [self creatCollectionView];

    
    
    
    //    [self creatTitleCollection];
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
